package com.atguigu.book.dao.impl;

import com.atguigu.book.dao.OrderDAO;
import com.atguigu.book.dao.OrderItemDAO;
import com.atguigu.book.pojo.Order;
import com.atguigu.book.pojo.OrderItem;
import com.atguigu.book.pojo.User;
import com.atguigu.myssm.basedao.BaseDAO;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName: OrderDAOImpl
 * @Description:
 * @Author: wty
 * @Date: 2022/12/13
 */

public class OrderDAOImpl extends BaseDAO<Order> implements OrderDAO {

    @Override
    public void addOrder(Order order) {
        String sqlStr = "INSERT INTO bookdb.`t_order` VALUES(0,?,?,?,?,?)";
        /**
         * executeUpdate方法的返回值：主键的最大值
         * int count = this.psmt.executeUpdate();
         *             if (insertFlag) {
         *                 this.rs = this.psmt.getGeneratedKeys();
         *                 if (this.rs.next()) {
         *                     return Long.valueOf(this.rs.getLong(1)).intValue();
         *                 }
         *             }
         */
        int orderId = executeUpdate(sqlStr, order.getOrderNo(), order.getOrderDate(), order.getOrderUser().getId(), order.getOrderMoney(), order.getOrderStatus());
        order.setId(orderId);
    }

    @Override
    public List<Order> getOrderList(User user) {
        String sqlStr = "SELECT * FROM bookdb.`t_order` a WHERE a.orderUser = ?";
        return executeQuery(sqlStr, user.getId());
    }

    @Override
    public Integer getOrderTotalBookeCount(Order order) {
        String sqlStr = "SELECT SUM(b.`buyCount`) totalBookeCount,b.`orderBean` AS `order`  FROM bookdb.`t_order` a, bookdb.`t_order_item` b WHERE a.`id` = b.`orderBean` AND a.orderUser = ? AND b.`orderBean` = ? GROUP BY b.`orderBean` ";
        return ((BigDecimal) executeComplexQuery(sqlStr, order.getOrderUser().getId(), order.getId())[0]).intValue();
    }
}
